## The code below replicates Figure 1 from 
## Yuree Noh and Marwa Shalaby, "Who Supports Gender Quotas in Transitioning and Authoritarian States in the Middle East and North Africa?" Comparative Political Studies
## Using the Arab Barometer Wave 5 data. 

############################
## Load Data and Packages ##
############################

rm(list=ls())
setwd("~/Dropbox/Research/quotasupport/data/ab")
ab5 = read.csv("ABV_Release_Data.csv", colClasses = "character")

library(dplyr)
library(ggplot2)
library(ggcharts)

####################################
# clean variables
####################################

ab5$quota = ifelse(ab5$Q601A == 1 | ab5$Q601A == 2, 1, 0)
ab5$quota = ifelse(is.na(ab5$quota) == T, 0, ab5$quota)
table(ab5$quota)

ab5$ctyname <- ifelse(ab5$country == "1", "Algeria",
                      ifelse(ab5$country == "5", "Egypt",
                             ifelse(ab5$country == "7", "Iraq",
                                    ifelse(ab5$country == "8", "Jordan",
                                           ifelse(ab5$country == "9", "Kuwait",
                                                  ifelse(ab5$country == "10", "Lebanon",
                                                         ifelse(ab5$country == "11", "Libya",
                                                                ifelse(ab5$country == "13", "Morocco",
                                                                       ifelse(ab5$country == "15", "Palestine",
                                                                              ifelse(ab5$country == "19", "Sudan",
                                                                                     ifelse(ab5$country == "21", "Tunisia",
                                                                                            ifelse(ab5$country == "22", "Yemen",NA))))))))))))

tab = ab5 %>%
  group_by(ctyname) %>%
  count(quota) %>%
  mutate(Freq = n/sum(n))
tab$Freq = tab$Freq*100
tab <- subset(tab, tab$quota==1)
tab$quota = c(1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0)

tab %>%
  bar_chart(ctyname, Freq) + ylab("") + xlab("Country") + ylim(NA, 90) + geom_bar(stat="identity") + geom_text(aes(label = sprintf("%0.f", Freq), hjust = 1.3), color = "white", size = 3) + scale_y_continuous(labels = scales::percent_format(scale = 1)) + theme_bw() + theme(panel.grid.major.y = element_blank(), panel.grid.minor.x = element_blank())
